<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="库房名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="库房名称"></el-input>
    </el-form-item>
    <el-form-item label="所属科室" prop="department">
      <el-select v-model="dataForm.department">
        <el-option value="病区药房">病区药房</el-option>
        <el-option value="门诊药房">门诊药房</el-option>
        <el-option value="药剂科">药剂科</el-option>
        <el-option value="麻醉科">麻醉科</el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="出库方式" prop="deliveryMode">
      <el-select v-model="dataForm.deliveryMode">
        <el-option value="先进先出">先进先出</el-option>
        <el-option value="按批先进先出">按批先进先出</el-option>
      </el-select>
    </el-form-item>


      <el-form-item label="上午上班">
        <el-time-select
          v-model="dataForm.morningWorkTime"
          :picker-options="{
              start: '00:00',
              step: '00:15',
              end: '24:00'
            }"
          placeholder="选择时间">
        </el-time-select>
      </el-form-item>
      <el-form-item label="上午下班">
        <el-time-select
          v-model="dataForm.morningCloseTime"
          :picker-options="{
              start: '00:00',
              step: '00:15',
              end: '24:00'
            }"
          placeholder="选择时间">
        </el-time-select>
      </el-form-item>
      <el-form-item label="下午上班">
        <el-time-select
          v-model="dataForm.afternoonWorkTime"
          :picker-options="{
              start: '00:00',
              step: '00:15',
              end: '24:00'
            }"
          placeholder="选择时间">
        </el-time-select>
      </el-form-item>
      <el-form-item label="下午下班">
        <el-time-select
          v-model="dataForm.afternoonCloseTime"
          :picker-options="{
              start: '00:00',
              step: '00:15',
              end: '24:00'
            }"
          placeholder="选择时间">
        </el-time-select>
      </el-form-item>
    <el-form-item label="发药对象" prop="hairMedicine">
      <el-select v-model="dataForm.hairMedicine">
        <el-option value="门诊">门诊</el-option>
        <el-option value="住院">住院</el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="管理药品" prop="managementDrugs">
      <el-select v-model="managementDrugsVal" multiple>
        <el-option value="西药">西药</el-option>
        <el-option value="中草药">中草药</el-option>
        <el-option value="中成药">中成药</el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="拼音码" prop="pnumber">
      <el-input v-model="dataForm.pnumber" placeholder="拼音码"></el-input>
    </el-form-item>
    <el-form-item label="自定义码" prop="znumber">
      <el-input v-model="dataForm.znumber" placeholder="自定义码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getHisStorehouseObj, addHisStorehouseObj, putHisStorehouseObj} from '@/api/his/hisstorehouse'

    export default {
    data () {
      return {
        managementDrugsVal: [],
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          department: '',
          deliveryMode: '',
          morningWorkTime: '',
          morningCloseTime: '',
          afternoonWorkTime: '',
          afternoonCloseTime: '',
          hairMedicine: '',
          managementDrugs: '',
          pnumber: '',
          znumber: '',
          memo: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          tenantId: '',
          orgId: '',
          unitId: '',
        },
        dataRule: {
          name: [
            { required: true, message: '库房名称不能为空', trigger: 'blur' }
          ],
          department: [
            { required: true, message: '库房所属科室不能为空', trigger: 'blur' }
          ],
          deliveryMode: [
            { required: true, message: '出库方式不能为空', trigger: 'blur' }
          ],
          morningWorkTime: [
            { required: true, message: '上午上班时间不能为空', trigger: 'blur' }
          ],
          morningCloseTime: [
            { required: true, message: '上午下班时间不能为空', trigger: 'blur' }
          ],
          afternoonWorkTime: [
            { required: true, message: '下午上班时间不能为空', trigger: 'blur' }
          ],
          afternoonCloseTime: [
            { required: true, message: '下午下班时间不能为空', trigger: 'blur' }
          ],
          hairMedicine: [
            { required: true, message: '发药对象不能为空', trigger: 'blur' }
          ],
          managementDrugs: [
            { required: true, message: '管理药品不能为空', trigger: 'blur' }
          ],
          pnumber: [
            { required: true, message: '拼音码不能为空', trigger: 'blur' }
          ],
          znumber: [
            { required: true, message: '自定义码不能为空', trigger: 'blur' }
          ],
          memo: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建用户id不能为空', trigger: 'blur' }
          ],
          modifyUser: [
            { required: true, message: '修改用户id不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户id不能为空', trigger: 'blur' }
          ],
          orgId: [
            { required: true, message: '组织id不能为空', trigger: 'blur' }
          ],
          unitId: [
            { required: true, message: '单位id不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '逻辑删除(1-正常,-1已删除)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getHisStorehouseObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
                this.managementDrugsVal = this.dataForm.managementDrugs.split(',');
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
       this.dataForm.managementDrugs = this.managementDrugsVal.toString();
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putHisStorehouseObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addHisStorehouseObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
